package com.bmadmin.controller.form;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.bmadmin.domain.Datagrid;
import com.bmadmin.domain.MessageDomain;
import com.bmadmin.domain.NormalResult;
import com.bmadmin.domain.SjkcxDomain;
import com.bmadmin.domain.SysUsersPOJO;
import com.bmadmin.service.SjkCxService;
import com.bmadmin.service.SysUsersService;
import com.bmadmin.util.BMUtil;
import com.bmadmin.util.Constants;
import com.bmadmin.util.Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/sjkcx")
public class SjkCxController {
	private Logger logger = LogManager.getLogger(this.getClass().getName());

    @Autowired
    SjkCxService sjkcxService;
    /**
     * 获取数据库查询申请表列表
     */
	@GetMapping("/getSjkcxList")
    public Object getSjkcxList(HttpSession session,SjkcxDomain sjkcxdomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", sjkcxdomain.getQs_date());
		map.put("jz_date", sjkcxdomain.getJz_date());
		if(sjkcxdomain.getReq_state()==null){
			map.put("req_state", "0,3,4");//默认显示表单状态 0--未提交 3--审核未通过 4--等待验证
		}else if("".equals(sjkcxdomain.getReq_state())){
			map.put("req_state", "0,1,2,3,4,5");
		}else{
			map.put("req_state", sjkcxdomain.getReq_state());
		}
		map.put("userid", user.getU_id());
		logger.info(sjkcxdomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<SjkcxDomain> list = sjkcxService.getSjkcxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 获取登录人信息
     */
    @GetMapping("/getdlrxx")
    public NormalResult getdlrxx(HttpSession session){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	Map map = new HashMap();
    	map.put("u_id", user.getU_id());
    	SjkcxDomain sjkcxdomain = sjkcxService.getDlrXx(map);
        NormalResult result = new NormalResult();
        result.setCode("0000");
        result.setMsg("success");
        result.setData(sjkcxdomain);
        return result;
    }
    
    /**
     * 申请单保存
     */
    @PostMapping("/insertSjkcx")
    public NormalResult insertSjkcx(HttpSession session,SjkcxDomain sjkcxdomain) {
    	String log = "数据库查询申请单保存>>>>>>>>>>>";
    	logger.info(log+"参数【"+sjkcxdomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_plane_time())){
        		result.setCode("9999");
                result.setMsg("预计使用时间为空！");
                logger.info(log+"预计使用时间为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_apply_phone())){
        		result.setCode("9999");
                result.setMsg("联系电话为空！");
                logger.info(log+"联系电话为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_title())){
        		result.setCode("9999");
                result.setMsg("需求标题为空！");
                logger.info(log+"需求标题为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_access())){
        		result.setCode("9999");
                result.setMsg("使用途径为空！");
                logger.info(log+"使用途径为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_explain())){
        		result.setCode("9999");
                result.setMsg("需求查询条件为空！");
                logger.info(log+"需求查询条件为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_output())){
        		result.setCode("9999");
                result.setMsg("输出内容为空！");
                logger.info(log+"输出内容为空！");
        	}
        	sjkcxdomain.setReq_apply_userid(user.getU_id());
        	sjkcxdomain.setReq_apply_depid(user.getD_id());
        	sjkcxdomain.setReq_apply_date(BMUtil.getNowTimeForString());
        	int retResult = sjkcxService.insertSjkcx(sjkcxdomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("保存成功！");
                logger.info(log+"保存成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("保存失败！");
                logger.info(log+"保存失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单保存异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单删除
     */
    @PostMapping("/deleteSjkcx")
    public NormalResult deleteSjkcx(SjkcxDomain sjkcxdomain) {
        NormalResult result = new NormalResult();
        if (BMUtil.isNullOrEmpty(sjkcxdomain.getReq_id())) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }

        result = sjkcxService.deleteSjkcx(sjkcxdomain.getReq_id());
        return result;
    }
    
    /**
     * 获取申请单信息
     */
    @GetMapping("/getOneSjkcx")
    public NormalResult getOneSjkcx(HttpSession session,String req_id){
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	NormalResult result = new NormalResult();
    	if (BMUtil.isNullOrEmpty(req_id)) {
            result.setCode("9999");
            result.setMsg("传入申请单ID为空!");
            return result;
        }
    	Map map = new HashMap();
//    	map.put("userid", user.getU_id());
    	map.put("req_id",Integer.parseInt(req_id));
    	List<SjkcxDomain> list = sjkcxService.getSjkcxList(map);
    	SjkcxDomain sjkcxdomain = list.get(0);
       
        result.setCode("0000");
        result.setMsg("success");
        result.setData(sjkcxdomain);
        return result;
    }
    
    /**
     * 申请单更新
     */
    @PostMapping("/updateSjkcx")
    public NormalResult updateSjkcx(HttpSession session,SjkcxDomain sjkcxdomain) {
    	String log = "数据库查询申请单更新>>>>>>>>>>>";
    	logger.info(log+"参数【"+sjkcxdomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_plane_time())){
        		result.setCode("9999");
                result.setMsg("预计使用时间为空！");
                logger.info(log+"预计使用时间为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_apply_phone())){
        		result.setCode("9999");
                result.setMsg("联系电话为空！");
                logger.info(log+"联系电话为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_title())){
        		result.setCode("9999");
                result.setMsg("需求标题为空！");
                logger.info(log+"需求标题为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_access())){
        		result.setCode("9999");
                result.setMsg("使用途径为空！");
                logger.info(log+"使用途径为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_explain())){
        		result.setCode("9999");
                result.setMsg("需求查询条件为空！");
                logger.info(log+"需求查询条件为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_output())){
        		result.setCode("9999");
                result.setMsg("输出内容为空！");
                logger.info(log+"输出内容为空！");
        	}
        	sjkcxdomain.setReq_apply_userid(user.getU_id());
        	sjkcxdomain.setReq_apply_depid(user.getD_id());
        	sjkcxdomain.setReq_apply_date(BMUtil.getNowTimeForString());
        	int retResult = sjkcxService.updateSjkcx(sjkcxdomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("更新成功！");
                logger.info(log+"更新成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("更新失败！");
                logger.info(log+"更新失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单更新异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单验证
     */
    @PostMapping("/upSjkcx")
    public NormalResult upSjkcx(SjkcxDomain sjkcxdomain) {
    	NormalResult result = new NormalResult();
    	try {
	        if (BMUtil.isNullOrEmpty(sjkcxdomain.getReq_id())) {
	            result.setCode("9999");
	            result.setMsg("传入申请单ID为空!");
	            return result;
	        }
	        sjkcxdomain.setReq_state("1");
	        result = sjkcxService.upSjkcx(sjkcxdomain);
	        return result;
    	}catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单提交异常！");
            logger.error(e);
            return result;
        }        
    }
    
    /**
     * 申请单验证
     */
    @PostMapping("/validateSjkcx")
    public NormalResult validateSjkcx(HttpSession session,SjkcxDomain sjkcxdomain) {
    	String log = "数据库查询申请单验证>>>>>>>>>>>";
    	logger.info(log+"参数【"+sjkcxdomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	int retResult = sjkcxService.updateSjkcx(sjkcxdomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("验证完成！");
                logger.info(log+"验证完成！");
        	}else{
        		result.setCode("9999");
                result.setMsg("验证失败！");
                logger.info(log+"验证失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单验证异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 申请单审核
     */
    @PostMapping("/verifySjkcx")
    public NormalResult verifySjkcx(HttpSession session,SjkcxDomain sjkcxdomain) {
    	String log = "数据库查询申请单审核>>>>>>>>>>>";
    	logger.info(log+"参数【"+sjkcxdomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_verify_view())){
        		result.setCode("9999");
                result.setMsg("申请部门意见为空！");
                logger.info(log+"申请部门意见为空！");
                return result;
        	}
        	
        	sjkcxdomain.setReq_verify_userid(user.getU_id());
        	sjkcxdomain.setReq_verify_date(BMUtil.getNowTimeForString());
        	int retResult = sjkcxService.verifySjkcx(sjkcxdomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("审核成功！");
                logger.info(log+"审核成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("审核失败！");
                logger.info(log+"审核失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单审核异常！");
            logger.error(e);
        }
        return result;
    }
    
    /**
     * 获取数据库查询申请表审核列表
     */
	@GetMapping("/getSjkcxShList")
    public Object getSjkcxShList(HttpSession session,SjkcxDomain sjkcxdomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", sjkcxdomain.getQs_date());
		map.put("jz_date", sjkcxdomain.getJz_date());
		if(sjkcxdomain.getReq_state()==null){
			map.put("req_state", "1");//默认显示表单状态 1--已提交未审核
		}else if("".equals(sjkcxdomain.getReq_state())){
			map.put("req_state", "1,2,3,4,5");
		}else{
			map.put("req_state", sjkcxdomain.getReq_state());
		}
		map.put("depid", user.getD_id());
		logger.info(sjkcxdomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<SjkcxDomain> list = sjkcxService.getSjkcxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 获取数据库查询申请表处理列表
     */
	@GetMapping("/getSjkcxClList")
    public Object getSjkcxClList(HttpSession session,SjkcxDomain sjkcxdomain,Integer page,Integer limit) {
    	Datagrid result = new Datagrid();
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
    	
    	Map map = new HashMap();
		map.put("qs_date", sjkcxdomain.getQs_date());
		map.put("jz_date", sjkcxdomain.getJz_date());
		if(sjkcxdomain.getReq_state()==null){
			map.put("req_state", "2,5");//默认显示表单状态 2--审核通过未处理 5--验证未通过 
		}else if("".equals(sjkcxdomain.getReq_state())){
			map.put("req_state", "2,4,5");
		}else{
			map.put("req_state", sjkcxdomain.getReq_state());
		}
		logger.info(sjkcxdomain);//列表条件输出
		
		PageHelper.startPage(page, limit);
		List<SjkcxDomain> list = sjkcxService.getSjkcxList(map);
		PageInfo pagelist = new PageInfo(list);
		
		result.setCode(0);
        result.setMsg("success");
        result.setData(list);
        result.setCount(pagelist.getTotal());

        return result;
    }
	
    /**
     * 申请单处理
     */
    @PostMapping("/disposeSjkcx")
    public NormalResult disposeSjkcx(HttpSession session,SjkcxDomain sjkcxdomain) {
    	String log = "数据库查询申请单处理>>>>>>>>>>>";
    	logger.info(log+"参数【"+sjkcxdomain+"】");
    	SysUsersPOJO user=(SysUsersPOJO) session.getAttribute(Constants.USER_INFO_SESSION);
        NormalResult result = new NormalResult();
        try {
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_id())){
        		result.setCode("9999");
                result.setMsg("申请单ID为空！");
                logger.info(log+"申请单ID为空！");
                return result;
        	}
        	if(!BMUtil.isNotNull(sjkcxdomain.getReq_dispose_view())){
        		result.setCode("9999");
                result.setMsg("结果与说明为空！");
                logger.info(log+"结果与说明为空！");
                return result;
        	}
        	
        	sjkcxdomain.setReq_dispose_userid(user.getU_id());
        	sjkcxdomain.setReq_dispose_date(BMUtil.getNowTimeForString());
        	int retResult = sjkcxService.disposeSjkcx(sjkcxdomain);
        	if(retResult>0){
        		result.setCode("0000");
                result.setMsg("处理成功！");
                logger.info(log+"处理成功！");
        	}else{
        		result.setCode("9999");
                result.setMsg("处理失败！");
                logger.info(log+"处理失败！");
        	}
        	return result;
        } catch (Exception e) {
            result.setCode("9999");
            result.setMsg("申请单处理异常！");
            logger.error(e);
        }
        return result;
    }
}
